/**
 * 获取app对象 - 全局应用对象
 */
const app = getApp();

/**
 * 过滤器 - filter 
 */
const filter = require('../../utils/filter');

/**
 * 做登录检测
 */
Page(filter.loginCheck({

	/**
	 * 页面的初始数据
	 */
	data: {
		userID: '',
		update: null,
		deptArray: [],
		deptIndex: 0,
		subjectArray: [],
		subjectIndex: 0
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

    /**
     * 数据存入
     */
		this.setData({
			userID: options.userid
		});
		let that = this;

    /**
     * 发送请求
     */
		wx.request({
			url: `${app.globalData.apiUrl}/getContactWhenUpdate/${that.data.userID}`,
			data: {},
			method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			/**
       * 对请求头做处理 - 设置请求的 header
       */
      // header: {},
			success: function (res) {
				// success
				if (res.data.success) {
					that.setData({
            //将数据update进去
						update: res.data.data,
						deptArray: res.data.depts,
						subjectArray: res.data.subjects,
						deptIndex: that.getIndexOfObjectArray(res.data.depts, res.data.data.deptId),
						subjectIndex: that.getIndexOfObjectArray(res.data.subjects, res.data.data.subjectId)
					})
				}
			},
      /**
       * update失败
       */
			fail: function () {
				// fail
			},

      /**
       * update完成
       */
			complete: function () {
				// complete
			}
		})

	},

  /**
   * 绑定姓名输入框
   */
	bindNameInput: function (e) {
		let oldData = this.data.update;
		oldData.name = e.detail.value;
		this.setData({
			update: oldData
		});
	},

  /**
   * 绑定手机号输入框
   */
	bindPhoneInput: function (e) {
		let oldData = this.data.update;
		oldData.phone = e.detail.value;
		this.setData({
			update: oldData
		});
	},

  /**
   * 监听性别 - 改变 
   */
	genderChange: function (e) {
		let oldData = this.data.update;
		oldData.gender = e.detail.value;
		this.setData({
			update: oldData
		});
	},

  /**
   * 绑定部门选中器
   */
	bindDeptPickerChange: function (e) {
		this.setData({
			deptIndex: e.detail.value
		});
	},

  /**
   * 绑定科目选择器
   */
	bindSubjectPickerChange: function (e) {
		this.setData({
			subjectIndex: e.detail.value
		});
	},


	getIndexOfObjectArray(objArray, objId) {
		for (let [idx, obj] of Object.entries(objArray)) {
			if (obj.id === objId) {
				return idx;
			}
		}
		return -1;
	},

  /**
   * 确定是否更新
   */
	confirmUpdate: function (e) {
		wx.showLoading({
			mask: true
		})

    /**
     * post 方式将数据提交上去
     */
		wx.request({
			url: `${app.globalData.apiUrl}/updateContact/${this.data.userID}`,
			data: {
				name: this.data.update.name,
				gender: this.data.update.gender,
				phone: this.data.update.phone,
				deptID: this.data.deptArray[this.data.deptIndex].id,
				subjectID: this.data.subjectArray[this.data.subjectIndex].id
			},
			method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				// success
				wx.hideLoading();

        /**
         * 更新成功后1500ms 往回调转到user界面
         */
				if (res.data.success) {
					wx.showToast({
						title: '成功',
						icon: 'success',
						success: function () {
							setTimeout(() => {
								wx.switchTab({
									url: "/pages/user/user"
								});
							}, 1500)
						}
					})

				}
			},

      /**
       * 请求失败后的处理
       */
			fail: function () {
				// fail
			},

      /**
       * 请求完成后的处理
       */
			complete: function () {
				// complete
			}
		})
	}
}))